﻿using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Domain.Enums;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using System;
using System.Reflection;

namespace DongFangGuoKai.WMS.NodeBussiness
{
    public class NodeBussinessFactory
    {
        private static IList<NodeBaseBusiness> NodeBusinessList { get; set;  }

        public static IServiceProvider NodeServiceProvider { get; set;  }
        //private IServiceProvider ServiceProvider { get; }

        private static Assembly _assmebly;

        //加载业务节点类型
        public NodeBussinessFactory(IServiceProvider serviceProvider)
        {
            //ServiceProvider = serviceProvider;

            NodeBusinessList = new List<NodeBaseBusiness>();

            if (_assmebly == null)
            {
                _assmebly = Assembly.Load("DongFangGuoKai.WMS.NodeBussiness");
            }
            if (_assmebly == null)
            {
                throw new AggregateException("加载程序集‘DongFangGuoKai.WMS.NodeBussiness’失败");
            }

            Type baseType = typeof(NodeBaseBusiness);
            Type[] types = _assmebly.GetTypes();
            foreach (Type type in types)
            {
                if (type.IsSubclassOf(baseType))
                {

                    NodeBaseBusiness business = serviceProvider.GetService(type) as NodeBaseBusiness;
                    if (business == null)
                    {
                        continue;
                    }
                    NodeBusinessList.Add(business);
                }
            }
            NodeServiceProvider = serviceProvider;
        }

        //读取业务节点类型 

        //开始
        private NodeBaseBusiness GetBussinessnNode(EmDeviceAction deviceAction, EmInstructType instructType, EmWorkDeviceType workDeviceType, EmVersion version)
        {
            NodeBaseBusiness business = null;
            if (NodeBusinessList != null && NodeBusinessList.Count > 0)
            {
                Func<NodeBaseBusiness, bool> invoke = t => t.InstructType == instructType && t.WorkDeviceType == workDeviceType && t.DeviceAction == deviceAction ;
                business = NodeBusinessList.FirstOrDefault(invoke);
            }
            return business;
        }

        //结束
        private NodeBaseBusiness GetBussinessnNode(t_Instruct instruct)
        {
            NodeBaseBusiness business = null;
            if (NodeBusinessList != null && NodeBusinessList.Count > 0)
            {
                Func<NodeBaseBusiness, bool> invoke = t => t.InstructType == instruct.InstructType &&
                                                                t.WorkDeviceType == instruct.WorkDeviceType &&
                                                                t.DeviceAction == instruct.Action;
                business = NodeBusinessList.FirstOrDefault(invoke);
            }
            return business;
        }

        //执行业务节点类型
        public bool InstructStart(t_InterfaceLog interfaceLog, EmWorkDeviceType workDeviceType, EmInstructType instructType, EmDeviceAction deviceAction, EmVersion version, out string msg, params object[] data)
        {
            msg = "";
            NodeBaseBusiness business = GetBussinessnNode(deviceAction, instructType, workDeviceType, version);
            if (business == null)
            {
                msg = "未找到匹配的业务类";
                return false;
            }
            return business.CmdStart(interfaceLog, out msg, data);
        }
        public bool InstructFinish(t_InterfaceLog interfaceLog, t_Instruct instruct, out string msg)
        {
            msg = "";
            NodeBaseBusiness business = GetBussinessnNode(instruct);
            if (business == null)
            {
                msg = "未找到匹配的业务类：" + instruct.ToJson();
                return false;
            }
            return business.CmdFinished(interfaceLog, instruct, out msg);
        }




    }

    public enum EmVersion
    {
        V1,
        V2,
        V3
    }
}
